function [Vb,R0] = dreumInner5(H,impossible,K,tau,phi,PMarkov,rho,tctp,theta0,tolInner,Vbsavetp)
% Inner loop for calculating conditional value function (Vb)


% Set parameters, initialize result arrays
Nutp = size(tctp,1);
Vb = cell(1,tau);
s = theta0(end);
R0 = zeros(K,Nutp,tau);
phiv = cell(1,tau);

% Calculate conditional value function
for idx2 = 1:tau

	phiv{idx2} = dreumFlow4(H,K,phi,tctp,theta0,idx2);                      % Calculate flow utility
	Vb0 = Vbsavetp{idx2};                         
	inVb = Vb0 + 2*tolInner;                                                % Ensure loop initializes   
    
    while max(max(max(abs(inVb - Vb0)))) > tolInner

		inVb = Vb0;
	
		e_cdf = (repmat(inVb(1,:,:),[H,1,1])...
            - inVb(2:end,:,:))./repmat(phi(2:end,:),[1,1,Nutp]);            % Calculate preference shock
		CDF = [exp(-exp(-e_cdf(1:end-1,:,:)));                              % Calculate CDF of T1EV distribution
               1 - exp(-s*e_cdf(end,:,:))];                                 % Calculate CDF of exponential dist.
		CDF(repmat(impossible(2:end,:),[1,1,Nutp])) = 1;                    % Set CDF = 1 if hunt is impossible to win (so doesn't affect product)
		P0 = prod(CDF);                                                     % Calculate pp-only choice prob
        
        R = squeeze(inVb(1,:,:).*P0) + ...
			squeeze(integral(@(r)Rint2(r,H,impossible,Nutp,phi,s,inVb),...
            0,Inf,'ArrayValued',true,'AbsTol',1e-10,'RelTol',0));           % Calculate conditional value function

        for idx3 = 1:H+1  
			for idx4 = 1:K
				Vb0(idx3,idx4,:) = phiv{idx2}(idx3,idx4,:);
				for idx5 = 1:K 
					Vb0(idx3,idx4,:) = squeeze(Vb0(idx3,idx4,:)) ...
                        + squeeze(rho*R(idx5,:)'*PMarkov(idx4,idx5,idx3));  % Calculate Bellman equation
				end
			end
        end
    end
    
    R0(:,:,idx2) = R; 
	Vb{idx2} = Vb0;
	
end 
